package com.itheima.edu.info.manager.dao.impl;


import com.itheima.edu.info.manager.dao.BaseStudentDao;
import com.itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao {

    //创建长度为5的学生数组对象容器   static修饰的成员可以被多个StudentDao对象调用
    private static ArrayList<Student> students = new ArrayList<>();
    static {
        //静态方法块,初始化数据
        Student student = new Student("itheima001","张三","21岁","1999-09-09");
        students.add(student);
        System.out.println("----------student集合初始化完成----------");
    }

    public Student[] findAllStudents() {
        Student[] stus = new Student[students.size()];
        for (int i = 0; i < stus.length; i++) {
            stus[i] = students.get(i);
        }
        return stus;
    }

    public boolean addStudent(Student student) {
        students.add(student);
        return true;
    }

    public boolean delectStudent(String id) {
        //遍历学生数组
        for (int i = 0; i < students.size(); i++) {
            if ( students.get(i).getId().equals(id)) {
                students.remove(i);
                return true;
            }
        }
        //不存在返回false
        return false;
    }

    public Student selectStudent(String id) {
        int index = -1;
        //遍历学生数组
        for (int i = 0; i < students.size(); i++) {
            if ( students.get(i).getId().equals(id)) {
                index = i;
                break;
            }
        }

        return students.get(index);
    }

    public Student updataStudent(Student student) {
        Student stu = new Student();
        int index = -1;
        //遍历学生数组
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(student.getId())) {
                students.get(i).setName(student.getName());
                students.get(i).setAge(student.getAge());
                students.get(i).setBirthday(student.getBirthday());
                index = i;
                break;
            }
        }

        return students.get(index);
    }
    //
}
